package model

import (
	"time"
	"user_srv/basic/config"
)

// 用户滑动行为表
type XqUserSwipes struct {
	Id           int64     `gorm:"column:id;type:bigint;primaryKey;not null;" json:"id"`
	UserId       int64     `gorm:"column:user_id;type:bigint;comment:用户ID;not null;" json:"user_id"`                  // 用户ID
	TargetUserId int64     `gorm:"column:target_user_id;type:bigint;comment:被滑动用户ID;not null;" json:"target_user_id"` // 被滑动用户ID
	Action       int8      `gorm:"column:action;type:tinyint(1);comment:滑动动作 1:喜欢 0:无感;not null;" json:"action"`      // 滑动动作
	CreatedAt    time.Time `gorm:"column:created_at;type:timestamp;default:CURRENT_TIMESTAMP;" json:"created_at"`
}

func (x *XqUserSwipes) TableName() string {
	return "xq_user_swipes"
}

func (x *XqUserSwipes) Create() error {
	return config.DB.Create(&x).Error
}

func (x *XqUserSwipes) Delete(where XqUserSwipes) error {
	return config.DB.Where(where).Delete(&x).Error
}

func (x *XqUserSwipes) Update(where XqUserSwipes) error {
	return config.DB.Where(where).Updates(&x).Error
}

func (x *XqUserSwipes) GetByUserAndTarget(userId, targetUserId int64) (*XqUserSwipes, error) {
	var swipe XqUserSwipes
	err := config.DB.Where("user_id = ? AND target_user_id = ?", userId, targetUserId).First(&swipe).Error
	return &swipe, err
}

func (x *XqUserSwipes) List(where XqUserSwipes, limit, offset int) ([]XqUserSwipes, error) {
	var swipes []XqUserSwipes
	query := config.DB.Where(where)
	if limit > 0 {
		query = query.Limit(limit)
	}
	if offset > 0 {
		query = query.Offset(offset)
	}
	err := query.Find(&swipes).Error
	return swipes, err
}

// 检查是否已经滑动过
func (x *XqUserSwipes) HasSwiped(userId, targetUserId int64) bool {
	var count int64
	config.DB.Model(&XqUserSwipes{}).Where("user_id = ? AND target_user_id = ?", userId, targetUserId).Count(&count)
	return count > 0
}

// 根据用户ID获取滑动记录
func (x *XqUserSwipes) GetByUserIds(userId, targetUserId int64) (*XqUserSwipes, error) {
	var swipe XqUserSwipes
	err := config.DB.Where("user_id = ? AND target_user_id = ?", userId, targetUserId).First(&swipe).Error
	return &swipe, err
}
